<?php
/**
 * Desc： 数据表格组件
 * Author：Fly
 * DateTime：2020/9/12 10:57
 */

namespace FlyToper\PearBackend;

use FlyToper\PearBackend\libs\Utils;

class Table
{
    protected $table_id;

    //列表的默认配置
    protected $conf = [
        'even' => false,     //开启表格斑马线
        'page' => false,
        'autoSort' => false,
        'toolbar' => '#flytoper-table-toolbar',  //开启工具栏
        'defaultToolbar' => ['filter', 'exports'], //自定义列、打印、导出
        'height' => 'full-120',  //固定高度
        'text' => [
            'none' => '无数据',
        ],
        'pk' => 'id', //数据表主键，只支持单主键
        'laypage' => 0, //默认 不开启分页
        'curr_page' => 1, //默认第一页
        'limit' => 50, //默认没页50条数据
    ];

    /**
     * 列表表头对象
     * @var Header | null
     */
    protected $header = null;

    /**
     * 列表搜索表单对象
     * @var Search | null
     */
    protected $search = null;

    public function __construct($table_id = '')
    {
        if(!$table_id) {
            $num = rand(10, 99);
            $table_id = "flytoper-table-{$num}";

        }
        $this->table_id = $table_id;
        $this->setConf('elem', "#{$this->table_id}");
    }

    public function setConf($k, $v = '')
    {
        Utils::setConf($k, $v, $this->conf);
        return $this;
    }

    //设置列表搜索框对象
    public function setSearch(Search $search)
    {
        $this->search = $search;
        return $this;
    }

    //设置表头对象
    public function setHeader(Header $header)
    {
        $this->header = $header;
        return $this;
    }

    //全局定义常规单元格的最小宽度
    public function setCellMinWidth($w)
    {
        return $this->setConf('cellMinWidth', $w);
    }

    //设置皮肤
    public function setSkin($skin)
    {
        return $this->setConf('skin', $skin);
    }


    //Table 渲染
    public function render()
    {
        try {
            if(!$this->header) {
                throw new \Exception('请先设置Header对象（setHeader）');
            }


            $conf_json = json_encode($this->conf);


            return <<<TPL
<body class="pear-container">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="realName" placeholder="" class="layui-input">
                    </div>
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" placeholder="" class="layui-input">
                    </div>
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </form>
        </div>
    </div>
</body>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="{$this->table_id}" lay-filter="{$this->table_id}"></table>
    </div>
</div>

<script type="text/javascript">
    layui.user(['table', 'form', 'jquery'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        
        let conf = {$conf_json};
        table.render(conf);
    });


</script>
TPL;





        }catch (\Exception $e) {
            return Utils::ePrint($e);
        }
    }
}